﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Sunlit.Business.Migrations
{
    public partial class add_messages : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "LinkMan",
                table: "AbpUsers",
                maxLength: 32,
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "RegionId",
                table: "AbpUsers",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "AbpCommerceMessages",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreationTime = table.Column<DateTime>(nullable: false),
                    CreatorUserId = table.Column<long>(nullable: true),
                    LinkMan = table.Column<string>(maxLength: 32, nullable: true),
                    PhoneNumber = table.Column<string>(maxLength: 32, nullable: true),
                    Content = table.Column<string>(nullable: true),
                    CommerceId = table.Column<long>(nullable: false),
                    ReceiverId = table.Column<long>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AbpCommerceMessages", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AbpCommerceMessages_AbpCommerces_CommerceId",
                        column: x => x.CommerceId,
                        principalTable: "AbpCommerces",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AbpCommerceMessages_AbpUsers_ReceiverId",
                        column: x => x.ReceiverId,
                        principalTable: "AbpUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "AbpEnterpriseMessages",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreationTime = table.Column<DateTime>(nullable: false),
                    CreatorUserId = table.Column<long>(nullable: true),
                    LinkMan = table.Column<string>(maxLength: 32, nullable: true),
                    PhoneNumber = table.Column<string>(maxLength: 32, nullable: true),
                    Content = table.Column<string>(nullable: true),
                    EnterpriseId = table.Column<long>(nullable: false),
                    ReceiverId = table.Column<long>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AbpEnterpriseMessages", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AbpEnterpriseMessages_AbpEnterprises_EnterpriseId",
                        column: x => x.EnterpriseId,
                        principalTable: "AbpEnterprises",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AbpEnterpriseMessages_AbpUsers_ReceiverId",
                        column: x => x.ReceiverId,
                        principalTable: "AbpUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "AbpPolicyMessages",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreationTime = table.Column<DateTime>(nullable: false),
                    CreatorUserId = table.Column<long>(nullable: true),
                    LinkMan = table.Column<string>(maxLength: 32, nullable: true),
                    PhoneNumber = table.Column<string>(maxLength: 32, nullable: true),
                    Content = table.Column<string>(nullable: true),
                    PolicyId = table.Column<long>(nullable: false),
                    ReceiverId = table.Column<long>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AbpPolicyMessages", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AbpPolicyMessages_AbpPolicies_PolicyId",
                        column: x => x.PolicyId,
                        principalTable: "AbpPolicies",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AbpPolicyMessages_AbpUsers_ReceiverId",
                        column: x => x.ReceiverId,
                        principalTable: "AbpUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_AbpUsers_RegionId",
                table: "AbpUsers",
                column: "RegionId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpCommerceMessages_CommerceId",
                table: "AbpCommerceMessages",
                column: "CommerceId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpCommerceMessages_ReceiverId",
                table: "AbpCommerceMessages",
                column: "ReceiverId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpEnterpriseMessages_EnterpriseId",
                table: "AbpEnterpriseMessages",
                column: "EnterpriseId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpEnterpriseMessages_ReceiverId",
                table: "AbpEnterpriseMessages",
                column: "ReceiverId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpPolicyMessages_PolicyId",
                table: "AbpPolicyMessages",
                column: "PolicyId");

            migrationBuilder.CreateIndex(
                name: "IX_AbpPolicyMessages_ReceiverId",
                table: "AbpPolicyMessages",
                column: "ReceiverId");

            migrationBuilder.AddForeignKey(
                name: "FK_AbpUsers_AbpRegions_RegionId",
                table: "AbpUsers",
                column: "RegionId",
                principalTable: "AbpRegions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_AbpUsers_AbpRegions_RegionId",
                table: "AbpUsers");

            migrationBuilder.DropTable(
                name: "AbpCommerceMessages");

            migrationBuilder.DropTable(
                name: "AbpEnterpriseMessages");

            migrationBuilder.DropTable(
                name: "AbpPolicyMessages");

            migrationBuilder.DropIndex(
                name: "IX_AbpUsers_RegionId",
                table: "AbpUsers");

            migrationBuilder.DropColumn(
                name: "LinkMan",
                table: "AbpUsers");

            migrationBuilder.DropColumn(
                name: "RegionId",
                table: "AbpUsers");
        }
    }
}
